var Common = require('wy-Common')
cc.Class({
    extends: cc.Component,

    properties: {
        spanControl : require('SpanControl')
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.isIgnore = true;
        this.pathReady = this.node.getComponent('PathReady');
        this.launcher = this.node.getChildByName('launcher').getComponent('SkillLauncher')
        this.launcher.setSkillParentNode(global.GameLoop.GameMgr.no_BulletMonsterLayer)
        this.b_isDie = false;
        this.attack = 1;

        this.n_bossTime = 0;
        this.b_isBoss = true;
        global.Event.emit('SHOWBLOOD')
    },

    setOptions(option, mgr) {
          this.prefab=option.prefab;
        this.b=option.cfg.keepTime;
        this.logicOptions = option.cfg;
        this.logicIndex = 0;
        this.monsterMgr = mgr;
        this.currentLogic = this.logicOptions.status[this.logicIndex];
        this.blood = this.logicOptions.blood * option.grow//血量
        this.score = this.logicOptions.score; //击败后掉落的分数
        this.isIgnore = true;                 // 是否是无敌状态
        this.n_ignoreTime = 0;
        this.keepTime = this.logicOptions.keepTime;
        this.option = option;


        this.additional = option.additional;
        if (option.cfg.isBoss) {
            global.Event.emit('bossBattle')
        }
        this.randomAdditionTime();
    },

    playBombEffect() {
        var self = this;
        this.node.stopAllActions();
        global.Event.emit('onEventBassExit')
        // global.GameLoop.GameMgr.bgMgr.changeBg();
        this.scheduleOnce(function () {
            self.node.runAction(cc.sequence(cc.blink(1, 2), cc.callFunc(function () {
                self.node.active = false;
                if (global.gates != 0 && global.gates != -1 && self.big) {
                    global.GameLoop.GameMgr.creatBossPlane(1, self.node.x, self.node.y)
                    // global.GameLoop.GameMgr.startPlot('end', self._createNavagation.bind(self));
                } else {
                    self._createNavagation();
                }
            })))
        }, 1.5)
    },

    _createNavagation() {
        this.node.destroy();
        global.GameLoop.GameMgr.resumeGame();
    },

    onAttack(bullet) {
        this._super(bullet);
        this.bloodCount = 1;
        this.progress = this.blood / (this.logicOptions.blood * this.option.grow);
        this.big = false;
        if (this.node.children[0].name == 'big') {
            this.big = true;
            if (this.blood > this.logicOptions.blood * this.option.grow / 2) {
                this.progress = (this.blood - this.logicOptions.blood * this.option.grow / 2) / (this.logicOptions.blood * this.option.grow / 2);
            } else {
                this.progress = this.blood / (this.logicOptions.blood * this.option.grow / 2);
                this.bloodCount = 2;
            }
        }
        var p = {
            'progress': this.progress,
            'bloodCount': this.bloodCount,
            'isBig': this.big
        }
        global.Event.emit('bossBlood', p)
    },

    randomAdditionTime() {
        this.n_bossTime = this.additional.time + Common.randomInteger();
    },

    gameUpdate(dt) {
        this._super(dt);
        if (this.b_isDie) {
            return;
        }
        this.n_bossTime -= dt;
        if (this.n_bossTime < 0) {
            this.n_bossTime = this.additional.time
            var len = Math.floor(this.additional.list.length * Math.random());
            this.monsterMgr.createEnemyTeam(this.additional.list[len])
        }
    },
    runLogic(logic){
        if(this.isIgnore != logic.isIgnore){
            this.isIgnore = logic.isIgnore;
            this.node.emit('monster-ignore',this)
        }

        if(logic.path){
            var path = global.PathConfig[logic.path];
            this.pathReady.setFlip(logic.pathFlip.x,logic.pathFlip.y)
            this.pathReady.loadData(path);
            this.pathReady.runAction(1)
        }
        this.launcher.reset();
        if(logic.skill){
            var r = 0;
            while(logic.skill[r]){
                this.launcher.addLauncher(logic.skill[r])
                r++;
            }
        }
        if(logic.span && this.spanControl){
            this.spanControl.playDBArmature(logic.span)
        }
        if(logic.action){
            this.runCCAction(logic.action)
        }   
    },
    setIgnore(r,time){ //设置对象无敌
        this.isIgnore = r;
        this.node.emit('monster-ignore',this)
        this.n_ignoreTime = time;
    },

    runCCAction(data){
        var arr = data.split(',')
        var type = arr[0];
        var action = null;
        if(type == 'r'){
            this.node.getChildByName('body').rotation = arr[2]
            action = cc.rotateTo(arr[1],arr[3]);
            this.node.getChildByName('body').runAction(action)
        } else if(type == 'mb'){
            action = cc.moveBy(arr[1],cc.v2(arr[2],arr[3]));
            this.node.runAction(action)
        } else if(type == 'mt'){
            action = cc.moveTo(arr[1],cc.v2(arr[2],arr[3]));
            this.node.runAction(action)
        }
    },
    playBombEffect(){
        var self = this;
        this.node.stopAllActions();
        this.node.runAction(cc.fadeOut(0.3))
        this.scheduleOnce(function(){
            self.node.destroy();
        },0.5)
    },
    onAttack(bullet){ //受到攻击
        // cc.log('=======',bullet.node.x)
        if(!bullet.attack){
            cc.warn('子弹没有伤害?????????????')
        }
        if(this.isIgnore){ //无敌状态 
            return;
        }
        this.blood -= bullet.getAttack();
        // cc.log(this.node.name,',血量剩下:',this.blood)
        if(this.blood <=0 ){
            if(this.prefab.slice(0,4)=='boss'){
                SoundMgr.playSound('Audio/Explode_l',false,1);
            }else{
                // SoundMgr.playSound('Audio/boom1',false,0.7);
                // global.GameLoop.GameMgr.sound.playBomb()
            }
            this.monsterMgr.destroyMonster(this);
            this.onDisable();
            this.b_isDie = true;
            this.launcher.reset();
            this.playBombEffect()
            
            global.GameLoop.GameMgr.particleMgr.createCoin(this.score,this);
        } else if(bullet.hit){
            var hit=global.Loader.getInstantiate('game/hit');
            this.node.addChild(hit);
            hit.x=bullet.node.x-this.node.x; 
            hit.y=-this.node.height/2;
            hit.js.setOptions(bullet.hit);
        }
    },
    onColliderEnter(collider){ //如果飞机接收到了碰撞事件 一定是飞机间的
        collider.onAttack(this);
    },
    onDestroy(){
        this.node.emit('child-removed',this.node);
    },
    gameUpdate(dt){
        if(this.n_ignoreTime && this.n_ignoreTime>0){
            this.n_ignoreTime -= dt;
            if(this.n_ignoreTime <= 0){
                this.setIgnore(!this.isIgnore);
            }
        }
        if(this.b_isDie){
            return;
        }
        if(this.launcher){
            this.launcher.gameUpdate(dt);
        }
        if(!this.currentLogic){
            return;
        }
        if(!this.currentLogic.delay){
            this.currentLogic.delay = -1
            this.runLogic(this.currentLogic);
            return
        } else if(this.currentLogic.delay>=0){
            this.currentLogic.delay -= dt;
            if(this.currentLogic.delay < 0){
                this.runLogic(this.currentLogic);
            }
            return;
        }
        if(this.currentLogic.time >= 0){
            this.currentLogic.time -= dt;
            if(this.currentLogic.time < 0){
                if(this.currentLogic.to != undefined){
                    this.logicIndex = this.currentLogic.to
                } else {
                    this.logicIndex++;
                }
                this.currentLogic = global.Common.deepCopy(this.logicOptions.status[this.logicIndex])
                if(!this.currentLogic){
                    // cc.log('逻辑完成')
                    this.node.destroy();
                }
            }
        }
    }
});
